package com.chapter17;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

public class FutureValueActionEvents implements ActionListener
  {
   private FutureValueSepPanel panel;

   public FutureValueActionEvents (FutureValueSepPanel p)
     {
      this.panel = p;
     }

   public void actionPerformed (ActionEvent e)
     {
      Object source = e.getSource();
      if (source == panel.exitButton)
        System.exit(0);
      else
    	{
    	 if (source == panel.calculateButton)
           {
            double       payment     = Double.parseDouble(panel.paymentTextField.getText());
            double       rate        = Double.parseDouble(panel.rateTextField.getText());
            int          years       = Integer.parseInt(panel.yearsTextField.getText());
            double       futureValue = FinancialCalculations.calculateFutureValue(payment, rate, years);
            NumberFormat currency    = NumberFormat.getCurrencyInstance();
            panel.futureValueTextField.setText(currency.format(futureValue));
           }
        }
     }
  }
